package com.quanquan.user.service.statisticsReport;

import com.quanquan.user.entity.User;
import com.quanquan.user.repository.UserRepository;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述:新用户数据统计（日，周，月，年）
 *
 * @ClassName: UserStatisticsReportService
 * @Author: xiaoqianh丶
 * @Date: 2019-05-28 10:05
 * @Version: V1.0
 */
@Service
public class UserStatisticsReportService {


    private final UserRepository userRepository;

    public UserStatisticsReportService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    /**
     * 按时间查询（报表使用）
     * @param startTime
     * @param endTime
     * @return
     */
    public List<User> statisticalReports(Date startTime, Date endTime) {
        return userRepository.findAll((Specification<User>) (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();

                predicates.add(cb.greaterThanOrEqualTo(root.get("createTime").as(Date.class), startTime));
                predicates.add(cb.lessThanOrEqualTo(root.get("createTime").as(Date.class), endTime));


            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        });
    }
}
